1 //+------------------------------------------------------------------+
\r
2 //| Display Ccy Info.mq4 |
\r
3 //+------------------------------------------------------------------+
\r
5 #property indicator_chart_window
\r
7 //extern string Currencies = "AUD,CAD,CHF,EUR,GBP,JPY,NZD,USD,TRY,SGD,DKK,HKD,NOK,SEK,PLN,HUF,CZK,ZAR";
\r
8 //extern string CurrencyPairs = "AUD,CAD,CHF,EUR,GBP,JPY,NZD,USD";
\r
9 extern string CurrencyPairs = "GU";
\r
10 extern string FontName = "Courier New";
\r
11 extern int FontSize = 9;
\r
12 extern color FontColor = White;
\r
13 extern string OutputFormat = ",R-5.1";
\r
14 extern int HorizPos = 800;
\r
15 extern double VertPos = 20;
\r
16 extern double VertSpacing = 20;
\r
17 extern int ATRperiod1 = 14;
\r
18 extern int ATRperiod2 = 100;
\r
20 double spr, pnt, tickval;
\r
22 string CP[40], IndiName;
\r
24 //+------------------------------------------------------------------+
\r
26 //+------------------------------------------------------------------+
\r
27 CurrencyPairs = StringUpper(CurrencyPairs);
\r
28 if (CurrencyPairs == "") CurrencyPairs = Symbol();
\r
29 if (StringSubstr(CurrencyPairs,StringLen(CurrencyPairs)-1,1) != ",") CurrencyPairs = CurrencyPairs + ",";
\r
30 ccCP = StringFindCount(CurrencyPairs,",");
\r
31 for (int i=0; i<40; i++)
\r
34 for (i=0; i<40; i++) {
\r
35 int comma2 = StringFind(CurrencyPairs,",",comma1+1);
\r
36 string temp = StringSubstr(CurrencyPairs,comma1+1,comma2-comma1-1);
\r
37 CP[i] = ExpandCcy(temp);
\r
38 if (comma2 >= StringLen(CurrencyPairs)-1) break;
\r
43 string str = CurrencyPairs;
\r
44 for (i=0; i<StringLen(str); i++)
\r
45 checksum += i * StringGetChar(str,i);
\r
46 IndiName = "DisplayCcyInfo-" + checksum;
\r
47 IndicatorShortName(IndiName);
\r
51 //+------------------------------------------------------------------+
\r
53 //+------------------------------------------------------------------+
\r
58 //+------------------------------------------------------------------+
\r
66 //+------------------------------------------------------------------+
\r
67 //| Remove Objects |
\r
68 //+------------------------------------------------------------------+
\r
69 void RemoveObjects()
\r
72 while (k<ObjectsTotal()) {
\r
73 string objname = ObjectName(k);
\r
74 if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)
\r
75 ObjectDelete(objname);
\r
82 //+------------------------------------------------------------------+
\r
83 void PlotObjects() {
\r
84 //+------------------------------------------------------------------+
\r
86 for (int i=0; i<ccCP; i++) {
\r
88 if (ccy == "") continue;
\r
89 pnt = MarketInfo(ccy,MODE_POINT);
\r
90 dig = MarketInfo(ccy,MODE_DIGITS);
\r
91 spr = MarketInfo(ccy,MODE_SPREAD);
\r
92 tickval = MarketInfo(ccy,MODE_TICKVALUE);
\r
93 if (dig == 3 || dig == 5) {
\r
98 // string tstr1 = ccy + ": " + DoubleToStr(spr,1); // + " " + DoubleToStr(tickval,3);
\r
99 double atr1 = iATR(ccy,PERIOD_D1,ATRperiod1,0) / pnt;
\r
100 double atr2 = iATR(ccy,PERIOD_D1,ATRperiod2,0) / pnt;
\r
101 double range = (iHigh(ccy,PERIOD_D1,0) - iLow(ccy,PERIOD_D1,0))/pnt;
\r
102 string tstr1 = ccy + ": " + NumberToStr(atr1,OutputFormat) + " " + NumberToStr(atr2,OutputFormat) + " " + NumberToStr(range,OutputFormat);
\r
103 int yp = VertPos + i * VertSpacing;
\r
104 string objname = IndiName + "-" + i;
\r
105 ObjectCreate(objname,OBJ_LABEL,0,0,0);
\r
106 ObjectSet(objname,OBJPROP_XDISTANCE,xp);
\r
107 ObjectSet(objname,OBJPROP_YDISTANCE,yp);
\r
108 ObjectSetText(objname,tstr1,FontSize,FontName,FontColor);
\r
113 //+------------------------------------------------------------------+
\r
114 string NumberToStr(double n, string mask)
\r
115 //+------------------------------------------------------------------+
\r
116 // Formats a number using a mask, and returns the resulting string
\r
117 // Usage: string result = NumberToStr(number,mask)
\r
119 // Mask parameters:
\r
120 // n = number of digits to output, to the left of the decimal point
\r
121 // n.d = output n digits to left of decimal point; d digits to the right
\r
122 // -n.d = floating minus sign at left of output
\r
123 // n.d- = minus sign at right of output
\r
124 // +n.d = floating plus/minus sign at left of output
\r
125 // ( or ) = enclose negative number in parentheses
\r
126 // $ or £ or ¥ or € = include floating currency symbol at left of output
\r
127 // % = include trailing % sign
\r
128 // , = use commas to separate thousands
\r
129 // Z or z = left fill with zeros instead of spaces
\r
130 // R or r = round result in rightmost displayed digit
\r
131 // B or b = blank entire field if number is 0
\r
132 // * = show asterisk in leftmost position if overflow occurs
\r
133 // ; = switch use of comma and period (European format)
\r
134 // L or l = left align final string
\r
135 // T ot t = trim end result
\r
138 if (MathAbs(n) == 2147483647)
\r
141 mask = StringUpper(mask);
\r
143 int dot = StringFind(mask,".",0);
\r
145 dot = StringLen(mask);
\r
151 for (int i=0; i<dot; i++) {
\r
152 string char = StringSubstr(mask,i,1);
\r
153 if (char >= "0" && char <= "9") nleft = 10 * nleft + StrToInteger(char);
\r
156 for (i=dot+1; i<=StringLen(mask); i++) {
\r
157 char = StringSubstr(mask,i,1);
\r
158 if (char >= "0" && char <= "9") nright = 10 * nright + StrToInteger(char);
\r
160 nright = MathMin(nright,7);
\r
163 for (i=0; i<StringLen(mask); i++) {
\r
164 char = StringSubstr(mask,i,1);
\r
165 if (char >= "0" && char <= "9") {
\r
171 if (StringFind(mask,"$",0) >= 0) csym = "$";
\r
172 if (StringFind(mask,"£",0) >= 0) csym = "£";
\r
173 if (StringFind(mask,"€",0) >= 0) csym = "€";
\r
174 if (StringFind(mask,"¥",0) >= 0) csym = "¥";
\r
176 string leadsign = "";
\r
177 string trailsign = "";
\r
178 if (StringFind(mask,"+",0) >= 0 && StringFind(mask,"+",0) < dot) {
\r
180 if (n > 0) leadsign = "+";
\r
181 if (n < 0) leadsign = "-";
\r
183 if (StringFind(mask,"-",0) >= 0 && StringFind(mask,"-",0) < dot)
\r
184 if (n < 0) leadsign = "-"; else leadsign = " ";
\r
185 if (StringFind(mask,"-",0) >= 0 && StringFind(mask,"-",0) > dot)
\r
186 if (n < 0) trailsign = "-"; else trailsign = " ";
\r
187 if (StringFind(mask,"(",0) >= 0 || StringFind(mask,")",0) >= 0) {
\r
195 if (StringFind(mask,"%",0) >= 0) trailsign = "%";
\r
197 if (StringFind(mask,",",0) >= 0) bool comma = true; else comma = false;
\r
198 if (StringFind(mask,"Z",0) >= 0) bool zeros = true; else zeros = false;
\r
199 if (StringFind(mask,"B",0) >= 0) bool blank = true; else blank = false;
\r
200 if (StringFind(mask,"R",0) >= 0) bool round = true; else round = false;
\r
201 if (StringFind(mask,"*",0) >= 0) bool overf = true; else overf = false;
\r
202 if (StringFind(mask,"L",0) >= 0) bool lftsh = true; else lftsh = false;
\r
203 if (StringFind(mask,";",0) >= 0) bool swtch = true; else swtch = false;
\r
204 if (StringFind(mask,"T",0) >= 0) bool trimf = true; else trimf = false;
\r
206 if (round) n = MathFix(n,nright);
\r
210 for (i=0; i<StringLen(outstr); i++) {
\r
211 char = StringSubstr(outstr,i,1);
\r
212 if (char >= "0" && char <= "9") dleft++;
\r
213 if (char == ".") break;
\r
216 // Insert fill characters.......
\r
217 if (zeros) string fill = "0"; else fill = " ";
\r
219 outstr = "-" + StringRepeat(fill,nleft-dleft) + StringSubstr(outstr,1,StringLen(outstr)-1);
\r
221 outstr = StringRepeat(fill,nleft-dleft) + StringSubstr(outstr,0,StringLen(outstr));
\r
223 outstr = StringSubstr(outstr,StringLen(outstr)-9-nleft,nleft+1+nright-dotadj);
\r
225 // Insert the commas.......
\r
227 bool digflg = false;
\r
228 bool stpflg = false;
\r
231 for (i=0; i<StringLen(outstr); i++) {
\r
232 char = StringSubstr(outstr,i,1);
\r
233 if (char == ".") stpflg = true;
\r
234 if (!stpflg && (nleft-i == 3 || nleft-i == 6 || nleft-i == 9))
\r
235 if (digflg) out1 = out1 + ","; else out1 = out1 + " ";
\r
236 out1 = out1 + char;
\r
237 if (char >= "0" && char <= "9") digflg = true;
\r
241 // Add currency symbol and signs........
\r
242 outstr = csym + leadsign + outstr + trailsign;
\r
244 // 'Float' the currency symbol/sign.......
\r
247 bool fltflg = true;
\r
248 for (i=0; i<StringLen(outstr); i++) {
\r
249 char = StringSubstr(outstr,i,1);
\r
250 if (char >= "0" && char <= "9") fltflg = false;
\r
251 if ((char == " " && fltflg) || (blank && n == 0) ) out1 = out1 + " "; else out2 = out2 + char;
\r
253 outstr = out1 + out2;
\r
255 // Overflow........
\r
256 if (overf && dleft > nleft) outstr = "*" + StringSubstr(outstr,1,StringLen(outstr)-1);
\r
258 // Left shift.......
\r
260 int len = StringLen(outstr);
\r
261 outstr = StringLeftTrim(outstr);
\r
262 outstr = outstr + StringRepeat(" ",len-StringLen(outstr));
\r
265 // Switch period and comma.......
\r
268 for (i=0; i<StringLen(outstr); i++) {
\r
269 char = StringSubstr(outstr,i,1);
\r
270 if (char == ".") out1 = out1 + ","; else
\r
271 if (char == ",") out1 = out1 + "."; else
\r
272 out1 = out1 + char;
\r
277 if (trimf) outstr = StringTrim(outstr);
\r
281 //+------------------------------------------------------------------+
\r
282 string StringRepeat(string str, int n)
\r
283 //+------------------------------------------------------------------+
\r
284 // Repeats the string STR N times
\r
285 // Usage: string x=StringRepeat("-",10) returns x = "----------"
\r
287 string outstr = "";
\r
288 for(int i=0; i<n; i++) {
\r
289 outstr = outstr + str;
\r
294 //+------------------------------------------------------------------+
\r
295 string StringLeftTrim(string str)
\r
296 //+------------------------------------------------------------------+
\r
297 // Removes all leading spaces from a string
\r
298 // Usage: string x=StringLeftTrim(" XX YY ") returns x = "XX YY "
\r
301 string outstr = "";
\r
302 for(int i=0; i<StringLen(str); i++) {
\r
303 if (StringSubstr(str,i,1) != " " || !left) {
\r
304 outstr = outstr + StringSubstr(str,i,1);
\r
310 //+------------------------------------------------------------------+
\r
311 string StringUpper(string str)
\r
312 //+------------------------------------------------------------------+
\r
313 // Converts any lowercase characters in a string to uppercase
\r
314 // Usage: string x=StringUpper("The Quick Brown Fox") returns x = "THE QUICK BROWN FOX"
\r
316 string outstr = "";
\r
317 string lower = "abcdefghijklmnopqrstuvwxyz";
\r
318 string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
\r
319 for(int i=0; i<StringLen(str); i++) {
\r
320 int t1 = StringFind(lower,StringSubstr(str,i,1),0);
\r
322 outstr = outstr + StringSubstr(upper,t1,1);
\r
324 outstr = outstr + StringSubstr(str,i,1);
\r
329 //+------------------------------------------------------------------+
\r
330 string StringLower(string str)
\r
331 //+------------------------------------------------------------------+
\r
332 // Converts any uppercase characters in a string to lowercase
\r
333 // Usage: string x=StringUpper("The Quick Brown Fox") returns x = "the quick brown fox"
\r
335 string outstr = "";
\r
336 string lower = "abcdefghijklmnopqrstuvwxyz";
\r
337 string upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
\r
338 for(int i=0; i<StringLen(str); i++) {
\r
339 int t1 = StringFind(upper,StringSubstr(str,i,1),0);
\r
341 outstr = outstr + StringSubstr(lower,t1,1);
\r
343 outstr = outstr + StringSubstr(str,i,1);
\r
348 //+------------------------------------------------------------------+
\r
349 string StringTrim(string str)
\r
350 //+------------------------------------------------------------------+
\r
351 // Removes all spaces (leading, traing embedded) from a string
\r
352 // Usage: string x=StringUpper("The Quick Brown Fox") returns x = "TheQuickBrownFox"
\r
354 string outstr = "";
\r
355 for(int i=0; i<StringLen(str); i++) {
\r
356 if (StringSubstr(str,i,1) != " ")
\r
357 outstr = outstr + StringSubstr(str,i,1);
\r
362 //+------------------------------------------------------------------+
\r
363 double MathFix(double n, int d)
\r
364 //+------------------------------------------------------------------+
\r
365 // Returns N rounded to D decimals - works around a precision bug in MQL4
\r
367 return(MathRound(n*MathPow(10,d)+0.000000000001*MathSign(n))/MathPow(10,d));
\r
370 //+------------------------------------------------------------------+
\r
371 double DivZero(double n, double d)
\r
372 //+------------------------------------------------------------------+
\r
373 // Divides N by D, and returns 0 if the denominator (D) = 0
\r
374 // Usage: double x = DivZero(y,z) sets x = y/z
\r
376 if (d == 0) return(0); else return(n/d);
\r
379 //+------------------------------------------------------------------+
\r
380 int MathSign(double n)
\r
381 //+------------------------------------------------------------------+
\r
382 // Returns the sign of a number (i.e. -1, 0, +1)
\r
383 // Usage: int x=MathSign(-25) returns x=-1
\r
385 if (n > 0) return(1);
\r
386 else if (n < 0) return (-1);
\r
390 //+------------------------------------------------------------------+
\r
391 int StringFindCount(string str, string str2)
\r
392 //+------------------------------------------------------------------+
\r
393 // Returns the number of occurrences of STR2 in STR
\r
394 // Usage: int x = StringFindCount("ABCDEFGHIJKABACABB","AB") returns x = 3
\r
397 for (int i=0; i<StringLen(str); i++)
\r
398 if (StringSubstr(str,i,StringLen(str2)) == str2) c++;
\r
402 //+------------------------------------------------------------------+
\r
403 string ExpandCcy(string str)
\r
404 //+------------------------------------------------------------------+
\r
406 str = StringTrim(StringUpper(str));
\r
407 if (StringLen(str) < 1 || StringLen(str) > 2) return(str);
\r
409 for (int i=0; i<StringLen(str); i++) {
\r
410 string char = StringSubstr(str,i,1);
\r
411 if (char == "A") str2 = str2 + "AUD"; else
\r
412 if (char == "C") str2 = str2 + "CAD"; else
\r
413 if (char == "E") str2 = str2 + "EUR"; else
\r
414 if (char == "F") str2 = str2 + "CHF"; else
\r
415 if (char == "G") str2 = str2 + "GBP"; else
\r
416 if (char == "J") str2 = str2 + "JPY"; else
\r
417 if (char == "N") str2 = str2 + "NZD"; else
\r
418 if (char == "U") str2 = str2 + "USD"; else
\r
419 if (char == "H") str2 = str2 + "HKD"; else
\r
420 if (char == "S") str2 = str2 + "SGD"; else
\r
421 if (char == "Z") str2 = str2 + "ZAR";
\r